package com.example.ecommercemarketing.handler.impl;

import com.example.ecommercemarketing.dto.MarketingElementsDTO;
import com.example.ecommercemarketing.dto.ProductDTO;
import com.example.ecommercemarketing.dto.UserDTO;
import com.example.ecommercemarketing.handler.MarketingElementsHandler;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 通过销量获取营销素材
 */
public class SalesVolumeHandler implements MarketingElementsHandler {

    private static final int SALES_THRESHOLD = 1000;

    @Override
    public MarketingElementsDTO handle(ProductDTO product, UserDTO user) {
        if (product.getSalesVolume() == null || product.getSalesVolume() < SALES_THRESHOLD) {
            return null;
        }

        //这里直接写死了一些测试数据，实际项目中是通过发其它应用的接口、查数据库、经过一些繁琐的业务逻辑处理来获取数据的
        MarketingElementsDTO elements = new MarketingElementsDTO();
        elements.setRecommendText("【爆款热销】" + product.getName() + "，已售" + product.getSalesVolume() + "件！");
        elements.setProductHighlights("销量领先，大众之选");
        elements.setMarketingImageUrl("https://example.com/marketing/hot/" + product.getId() + ".jpg");
        elements.setExtraInfo("月销量超过" + SALES_THRESHOLD + "件");
        elements.setSource("sales_volume");

        Random random = new Random();
        int randomTime = random.nextInt(15);
        //模拟实际业务中的耗时
        try {
            TimeUnit.MILLISECONDS.sleep(randomTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return elements;
    }
}